package com.bringway.p2p.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bringway.global.dao.SysAttechmentDao;
import com.bringway.p2p.dao.POSDao;
import com.bringway.p2p.dao.ZqzrlbDao;
import com.bringway.p2p.service.POSService;

import net.sf.json.JSONObject;

/*
 * @ClassName: POSServiceImpl
 * @Description: pos的业务逻辑接口实现类
 * @author: 邵jing
 * @date: 2016年2月15日 下午3:58:53
 */
@Transactional
@Service
public class POSServiceImpl implements POSService {
	
	@Autowired
	private POSDao posDao;
	
	@Override
	public boolean savePosCode(JSONObject parms) {
		return posDao.savePosCode(parms)>0;
	}
	
	@Override
	public List searchPos(JSONObject parms) {
		return posDao.searchPos(parms);
	}
	
	@Override
	public int getTotalItem(JSONObject parms) {
		return posDao.getTotalItem(parms);
	}
	
	@Override
	public List posCodeInfoSearch(JSONObject parms) {
		return posDao.posCodeInfoSearch(parms);
	}

	@Override
	public int posCodeInfoSearchItems(JSONObject parms) {
		return posDao.posCodeInfoSearchItems(parms);
	}

	@Override
	public boolean posShenHe(JSONObject parms) {
		boolean bool = posDao.posShenHe1(parms);
		return bool;
	}

	@Override
	public List<HashMap<String,String>> queryPosCodeHkList(JSONObject params) {
		return posDao.queryPosCodeHkList(params);
	}

	@Override
	public int getPosCodeHkCount(JSONObject params) {
		return posDao.getPosCodeHkCount(params);
	}

	@Override
	public String savePosHk(JSONObject params) {
		posDao.savePosHk(params);
		posDao.updateUserZcxx(params);
		return "";
	}

	@Override
	public boolean isHasTzrxxByPoscode(JSONObject parms) {
		return posDao.isHasTzrxxByPoscode(parms);
	}

	@Override
	public boolean isReadyHk(JSONObject params) {
		return posDao.isReadyHk(params);
	}
	
	@Override
	public void deletePos(JSONObject params) {
		posDao.deletePos(params);
	}
	
	@Override
	public Map<String, Object> getPosInfo(JSONObject params) {
		return posDao.getPosInfo(params);
	}

	@Override
	public String isReadyPlHk(JSONObject params) {
		String res = "failure";
		String ids = params.optString("ids");
		if(StringUtils.isEmpty(ids)){
			res = "res_01";//数据空
		}else{
			String[] idArr = ids.split(",");
			for(int i = 0; i< idArr.length; i++){
				String tzrxxId = idArr[i];
				JSONObject params2 = new JSONObject();
				params2.put("tzrxxId", tzrxxId);
				boolean isReady = this.posDao.isReadyHk(params2);
				if(!isReady){
					res = "res_02_"+(++i);//此条记录还未到回款日
					return res;
				}
			}
			res = "success";
		}
		return res;
	}

	@Override
	public String savePosPlHk(JSONObject params) {
		String res = "failure";
		String ids = params.optString("ids");
		String posCodeArr = params.optString("posCodeArr");
		String tzjeArr = params.optString("tzjeArr");
		String tzsyjeArr = params.optString("tzsyjeArr");
		String hkContent = "批量回款";
		String hkUserId = params.optString("hkUserId");
		if(StringUtils.isEmpty(ids)){
			res = "res_01";//数据空
		}else{
			String[] idArr = ids.split(",");
			String[] posCode_Arr = posCodeArr.split(",");
			String[] tzje_Arr = tzjeArr.split(",");
			String[] tzsyje_Arr = tzsyjeArr.split(",");
			for(int i = 0; i< idArr.length; i++){
				String tzrxxId = idArr[i];
				String posCode = posCode_Arr[i];
				String tzje = tzje_Arr[i];
				String tzsyje = tzsyje_Arr[i];
				JSONObject params2 = new JSONObject();
				params2.put("tzrxxId", tzrxxId);
				params2.put("posCode", posCode);
				params2.put("tzje", tzje);
				params2.put("tzsyje", tzsyje);
				params2.put("hkContent", hkContent);
				params2.put("hkUserId", hkUserId);
				boolean isReady = this.posDao.isReadyHk(params2);
				if(isReady){
					this.posDao.savePosHk(params2);
					posDao.updateUserZcxx(params2);
				}else{
					res = "res_02_"+(++i);//此条记录还未到回款日
					return res;
				}
			}
			res = "success";
		}
		return res;
	}

	@Override
	public boolean isHasHkByPoscode(JSONObject params) {
		return posDao.isHasHkByPoscode(params);
	}

	@Override
	public String isHasPlHkByPoscode(JSONObject params) {
		String res = "failure";
		String ids = params.optString("ids");
		String posCodeArr = params.optString("posCodeArr");
		if(StringUtils.isEmpty(ids)){
			res = "res_01";//数据空
		}else{
			String[] idArr = ids.split(",");
			String[] posCode_Arr = posCodeArr.split(",");
			for(int i = 0; i< idArr.length; i++){
				String tzrxxId = idArr[i];
				String posCode = posCode_Arr[i];
				JSONObject params2 = new JSONObject();
				params2.put("tzrxxId", tzrxxId);
				params2.put("posCode", posCode);
				boolean isHasHk = this.posDao.isHasHkByPoscode(params2);
				if(isHasHk){
					res = "res_02_"+(++i);//此条记录已回款
					return res;
				}
			}
			res = "success";
		}
		return res;
	}


	@Override
	public boolean posZbShenHe(JSONObject params) {
		return posDao.posZbShenHe(params)>0;
	}


	@Override
	public boolean isRealCustomer(JSONObject parms) {
		return posDao.isRealCustomer(parms);
	}

	@Override
	public boolean ishasPoscodeInfo(JSONObject parms) {
		return posDao.ishasPoscodeInfo(parms);
	}

	@Override
	public List<Map<String,Object>> queryCzPoscodeTzrxxList(JSONObject params) {
		return posDao.queryCzPoscodeTzrxxList(params);
	}

	@Override
	public int getCzPoscodeTzrxxCount(JSONObject params) {
		return posDao.getCzPoscodeTzrxxCount(params);
	}

	@Override
	public void saveCxsq(JSONObject params) {
		posDao.saveCxsq(params);
	}

	@Override
	public boolean isHasCxsq(JSONObject params) {
		return posDao.isHasCxsq(params);
	}

	@Override
	public List<Map<String, Object>> queryCzPoscodeTzrxxZbshList(JSONObject params) {
		return posDao.queryCzPoscodeTzrxxZbshList(params);
	}

	@Override
	public int getCzPoscodeTzrxxZbshCount(JSONObject params) {
		return posDao.getCzPoscodeTzrxxZbshCount(params);
	}

	@Override
	public boolean isReadyCxsh(JSONObject params) {
		return posDao.isReadyCxsh(params);
	}

	@Override
	public void saveCxsh(JSONObject params) {
		posDao.saveCxsh(params);
	}
	
	

}
